package com.macbulldev.poc.testgin.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Version;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;


@javax.persistence.Entity
@Configurable
public class EventLog {
	
	private static final Logger LOGGER = Logger.getLogger(EventLog.class);

    private String objectClass;
    private Long objectId;

    private String operation;

    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(style = "S-")
    private java.util.Date createTs;

    private String createUsername;

	@PersistenceContext    
    transient EntityManager entityManager;

	@Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long id;

	@Version    
    @Column(name = "version")    
    private Integer version;

	public Long getId() {    
        return this.id;        
    }

	public void setId(Long id) {    
        this.id = id;        
    }

	public Integer getVersion() {    
        return this.version;        
    }

	public void setVersion(Integer version) {    
        this.version = version;        
    }
	
	

	public Long getObjectId() {
		return objectId;
	}

	public void setObjectId(Long objectId) {
		this.objectId = objectId;
	}

	@Transactional    
    public void persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.persist(this);        
    }

	@Transactional    
    public void remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        if (this.entityManager.contains(this)) {        
            this.entityManager.remove(this);            
        } else {        
            EventLog attached = this.entityManager.find(EventLog.class, this.id);            
            this.entityManager.remove(attached);            
        }        
    }

	@Transactional    
    public void flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }

	@Transactional    
    public void merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        EventLog merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }

	public static final EntityManager entityManager() {    
        EntityManager em = new EventLog().entityManager;        
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }

	public static long countEventLogs() {    
        return (Long) entityManager().createQuery("select count(o) from EventLog o").getSingleResult();        
    }

	public static List<EventLog> findAllEventLogs() {    
        return entityManager().createQuery("select o from EventLog o").getResultList();        
    }

	public static EventLog findEventLog(Long id) {    
        if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of EventLog");        
        return entityManager().find(EventLog.class, id);        
    }

	public List<EventLog> findByExample(Integer firstResult,Integer pageSize) {    
		
		boolean flag = false;
		String hql = "select el from EventLog el ";
		String whereClause=" where ";
		List<Object> params=new ArrayList<Object>();
		
		
		if(this.getObjectClass().length() > 0)
		{
			whereClause+=" el.objectClass = ? and ";
			params.add(this.getObjectClass());
		}
		if(this.getCreateUsername().length() > 0)
		{
			whereClause+=" el.createUsername = ? and ";
			params.add(this.getCreateUsername());
		}
		if(this.getOperation().length() > 0)
		{
			whereClause+=" el.operation= ?  and ";
			params.add(this.getOperation());
		}
		if(this.getId() != null)
		{
			whereClause+=" el.id= ? and ";
			params.add(this.getId());
		}
		if(this.getCreateTs() != null)
		{
			whereClause+=" el.createTs= ? and ";
				params.add(this.getCreateTs());
		}
				
		if (whereClause.endsWith("and "))
		{
			whereClause=whereClause.substring(0,whereClause.length()-4);
		}
		if (params.size()>0)
		{
			hql+= whereClause;
		}
		hql+=" order by el.createTs desc";
		
	
		//LOGGER.debug(">>  "+hql);
	
        Query q= entityManager().createQuery(hql);       
        
        int x=0;
        for (Object param : params)
        {
        	q.setParameter(++x, param);
        }
        q.setMaxResults(pageSize);
        q.setFirstResult(firstResult);
        return q.getResultList();
    }

	public Long countByExample() {    
		
		boolean flag = false;
		String hql = "select count(el) from EventLog el ";
		String whereClause=" where ";
		List<Object> params=new ArrayList<Object>();
		
		
		if(this.getObjectClass().length() > 0)
		{
			whereClause+=" el.objectClass = ? and ";
			params.add(this.getObjectClass());
		}
		if(this.getCreateUsername().length() > 0)
		{
			whereClause+=" el.createUsername = ? and ";
			params.add(this.getCreateUsername());
		}
		if(this.getOperation().length() > 0)
		{
			whereClause+=" el.operation= ?  and ";
			params.add(this.getOperation());
		}
		if(this.getId() != null)
		{
			whereClause+=" el.id= ? and ";
			params.add(this.getId());
		}
		if(this.getCreateTs() != null)
		{
			whereClause+=" el.createTs= ? and ";
				params.add(this.getCreateTs());
		}
				
		if (whereClause.endsWith("and "))
		{
			whereClause=whereClause.substring(0,whereClause.length()-4);
		}
		if (params.size()>0)
		{
			hql+= whereClause;
		}
	//	LOGGER.debug(">>  "+hql);
	
        Query q= entityManager().createQuery(hql);       
        
        int x=0;
        for (Object param : params)
        {
        	q.setParameter(++x, param);
        }
        return (Long)q.getResultList().get(0);
    }
	
	public static List<EventLog> findEventLogEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from EventLog o order by o.createTs desc").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }

	public String getObjectClass() {    
        return this.objectClass;        
    }

	public void setObjectClass(String objectClass) {    
        this.objectClass = objectClass;        
    }

	public String getOperation() {    
        return this.operation;        
    }

	public void setOperation(String operation) {    
        this.operation = operation;        
    }

	public Date getCreateTs() {    
        return this.createTs;        
    }

	public void setCreateTs(Date createTs) {    
        this.createTs = createTs;        
    }

	public String getCreateUsername() {    
        return this.createUsername;        
    }

	public void setCreateUsername(String createUsername) {    
        this.createUsername = createUsername;        
    }

	public String toString() {    
        StringBuilder sb = new StringBuilder();        
        sb.append("Id: ").append(getId()).append(", ");        
        sb.append("Version: ").append(getVersion()).append(", ");        
        sb.append("ObjectClass: ").append(getObjectClass()).append(", ");        
        sb.append("Operation: ").append(getOperation()).append(", ");        
        sb.append("CreateTs: ").append(getCreateTs()).append(", ");        
        sb.append("CreateUsername: ").append(getCreateUsername());        
        return sb.toString();        
    }
}
